const Router = require('koa-router')
const router = new Router()

const query = require('../db/query')

//查看
router.get('/get',async(ctx,next)=>{
    const sql = `select *from users`
    const result = await query(sql)
    ctx.body=result
})
//增加
router.post('/add',async(ctx,next)=>{
    const {username,pwd,age,phone,nickname} = ctx.request.body
    const sql = `insert into users(username,pwd,age,phone,nickname) values('${username}','${pwd}','${age}','${phone}','${nickname}')`
    const result = await query(sql)
    console.log(result)
    if(result.affectedRows == 1){
        ctx.body={
            code:200,
            msg:'success'
        }
    }
})

//删除
router.delete('/del',async(ctx,next)=>{
    const {id} = ctx.request.query
    const sql = `delete from users where id='${id}'`
    const result = await query(sql)
    console.log(result)
    if(result.affectedRows == 1){
        ctx.body={
            code:200,
            msg:'success'
        }
    } 
})

// 改
router.put('/update',async(ctx,next)=>{
    const {id,username,pwd,age,phone,nickname} = ctx.request.body
    if(!id){
        ctx.body={
            code:1,msg:'缺少id'
        }
    }
    const sql = `update users set username='${username}',pwd='${pwd}',age='${age}',phone'${phone}',nickname='${nickname}'`
    const result = await query(sql)
    console.log(result)
    if(result.affectedRows == 1){
        ctx.body={
            code:200,
            msg:'success'
        }
    }else {
        
    }
})
//数据添加
router.post('/post' , async(ctx,next)=>{
    const obj = ctx.request.body
    obj.id = Math.random()*999999|0
    //读取
    const reResult = await readFile(path.join(__dirname, './data.json'))
    const box = JSON.parse(reResult).student
    box.push(obj)
    //数据组装
    const wrRrsult = {student:box}
    //写入
    await writeFile('./data.json',JSON.stringify(wrRrsult))
    ctx.status=200
    ctx.body={code:200 , msg:"新增成功",data:null}
})

//数据查询
router.get('/get',async(ctx,next)=>{
    const obj = ctx.request.query
    if(obj.phone){//模糊查询
        const reResult = await readFile(path.join(__dirname, './data.json'))
        const result = JSON.parse(reResult).student.filter(item=>item.phone.includes(obj.phone))
        ctx.body=result
    }else {
        const reResult = await readFile(path.join(__dirname, './data.json'))
        if(JSON.parse(reResult).student.length<obj.size){
            ctx.body=JSON.parse(reResult).student
        }
    }
})

//数据修改
router.put('/put',async(ctx,next)=>{
    //缓存数据
    const obj = ctx.request.body 
    //读取数据
    const rResult = await readFile('./data.json')
    const parseResult = JSON.parse(rResult).student
    parseResult.forEach(item => {
        if(item.id == obj.id){
            obj.phone&&(item.phone = obj.phone)
            obj.username&&(item.username = obj.username)
        }
    });
    //数据组装
    const wrRrsult = {student:parseResult}
    //写入
    await writeFile('./data.json',JSON.stringify(wrRrsult))
    ctx.body={code:200 , msg:"修改成功",data:null}
})

//根据id 拿到要删除数据
router.delete('/del/:id',async(ctx,next)=>{
    let obj =ctx.request.params
    console.log(ctx.request.params);
    const rResult = await readFile('./data.json')
    const result = JSON.parse(rResult).student
    let i=null
    for (let j = 0; j < result.length; j++) {
        if(result[j].id == obj.id){
            i = j
            break
        }
    }
    //截取
    result.splice(i, 1)
    const wrRrsult = {student:result}
    await writeFile('./data.json',JSON.stringify(wrRrsult))
    ctx.body={code:200 , msg:"删除成功",data:null}
})
module.exports=router